package name.sraynal.coodiary.file.creator.http;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import name.sraynal.coodiary.file.creator.PageCreator;
import name.sraynal.coodiary.storage.Quote;

public class HttpStandardPageCreator extends PageCreator {

	@Override
	public void createPage(String type, List<Quote> quotesList, int pos,
			int pageNumber, File outputDir) throws IOException {
		File currentDir = new File(outputDir, type);
		currentDir.mkdir();
		File output = new File(currentDir, "page" + pos + ".htm");
		
		BufferedWriter writer = new BufferedWriter(new FileWriter(output));
		
		writer.append("<http>\n<head>\n<script type=\"text/javascript\" src=\"../arrows.js\" ></script>\n")
			  .append("<script type=\"text/javascript\" >\nvar pageNumber=" + pageNumber + "\n")
			  .append("var currentPage=" + pos + "</script>\n")
			  .append("</head>\n<body onkeydown=\"arrows(event)\">\n");
		writer.append("<tr><br><br><br><br><br><br>\n</tr>\n");
		
		writer.append(getLinksTable(pos, pageNumber));
		
		writer.append("<table border=\"0\" width=\"100%\">\n")
			  .append("<tr>\n<td width=\"25%\"></td>\n<td width=\"50%\">");
		
		for (int i = 0;i < quotesList.size(); i++) {
			if (i == 0) {
				writer.append("<p>\n");
			} else if (i%3 == 0) {
				writer.append("</p>\n<p>\n");
			}
			writer.append(quotesList.get(i).getValue()).append("<br>\n");
			
			if (i == quotesList.size() -1) {
				writer.append("</p>\n");
			}
		}
		writer.append("</td>\n<td width=\"25%\"></td>\n</tr>\n</table>\n");

		writer.append("</body>\n</http>");
		writer.close();
	}
}
